<%-- 
    Document   : staffMainPage
    Created on : Jul 8, 2012, 3:59:26 PM
    Author     : ThachKim
--%>

<%@page import="DTO.OptionForSelect"%>
<%@page import="org.w3c.dom.Node"%>
<%@page import="java.util.ArrayList"%>
<%@page import="javax.xml.xpath.XPathConstants"%>
<%@page import="org.w3c.dom.NodeList"%>
<%@page import="javax.xml.xpath.XPath"%>
<%@page import="javax.xml.xpath.XPathFactory"%>
<%@page import="java.io.File"%>
<%@page import="org.w3c.dom.Document"%>
<%@page import="javax.xml.parsers.DocumentBuilder"%>
<%@page import="javax.xml.parsers.DocumentBuilderFactory"%>
<%@page import="BLO.ProjectBLO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<style type="text/css">
    @import url("css/general.css");
</style>
<html>
    <head>
        <script type="text/javascript">
            function schange(form){
                var dayList = new Array(
                new Option("1","01"),new Option("2","02"),new Option("3","03"),
                new Option("4","04"),new Option("5","05"),new Option("6","06"),new Option("7","07"),
                new Option("8","08"),new Option("9","09"),new Option("10","10"),new Option("11","11"),
                new Option("12","12"),new Option("13","13"),new Option("14","14"),new Option("15","15"),
                new Option("16","16"),new Option("17","17"),new Option("18","18"),new Option("19","19"),
                new Option("20","20"),new Option("21","21"),new Option("22","22"),new Option("23","23"),
                new Option("24","24"),new Option("25","25"),new Option("26","26"),new Option("27","27"),new Option("28","28"));

                var yId=form.syearId;
                var chosenoption=yId.options[yId.selectedIndex]
                var y = chosenoption.value;

                var mId=form.smonthId;
                var chosenoption1=mId.options[mId.selectedIndex]
                var m = chosenoption1.value;

                if (m=="1" || m=="3"|| m=="5"|| m=="7"|| m=="8"|| m=="10"|| m=="12"){
                    dayList.push(new Option("29","29"));
                    dayList.push(new Option("30","30"));
                    dayList.push(new Option("31","31"));
                } else if (m=="4"|| m=="6"|| m=="9"|| m=="11"){
                    dayList.push(new Option("29","29"));
                    dayList.push(new Option("30","30"));
                } else if (y%4!=0 && m=="2"){
                    dayList.push(new Option("29","29"));
                }
                var myselect=form.sdayId
                while (myselect.length>0){
                    myselect.remove(myselect.length-1);
                }
                //add day
                for (var i = 0; i< dayList.length; i++){
                    try{
                        myselect.add(dayList[i], null);
                    }
                    catch(e){
                        myselect.add(dayList[i]);
                    }
                }
            }

            function echange(form){
                var dayList = new Array(
                new Option("1","01"),new Option("2","02"),new Option("3","03"),
                new Option("4","04"),new Option("5","05"),new Option("6","06"),new Option("7","07"),
                new Option("8","08"),new Option("9","09"),new Option("10","10"),new Option("11","11"),
                new Option("12","12"),new Option("13","13"),new Option("14","14"),new Option("15","15"),
                new Option("16","16"),new Option("17","17"),new Option("18","18"),new Option("19","19"),
                new Option("20","20"),new Option("21","21"),new Option("22","22"),new Option("23","23"),
                new Option("24","24"),new Option("25","25"),new Option("26","26"),new Option("27","27"),new Option("28","28"));

                var yId=form.eyearId;
                var chosenoption=yId.options[yId.selectedIndex]
                var y = chosenoption.value;

                var mId=form.emonthId;
                var chosenoption12 = mId.options[mId.selectedIndex]
                var m = chosenoption12.value;

                if (m=="1" || m=="3"|| m=="5"|| m=="7"|| m=="8"|| m=="10"|| m=="12"){
                    dayList.push(new Option("29","29"));
                    dayList.push(new Option("30","30"));
                    dayList.push(new Option("31","31"));
                } else if (m=="4"|| m=="6"|| m=="9"|| m=="11"){
                    dayList.push(new Option("29","29"));
                    dayList.push(new Option("30","30"));
                } else if (y%4!=0 && m=="2"){
                    dayList.push(new Option("29","29"));
                }
                var myselect=form.edayId;
                while (myselect.length>0){
                    myselect.remove(myselect.length-1);
                }
                //add day
                for (var i = 0; i< dayList.length; i++){
                    try{
                        myselect.add(dayList[i], null);
                    }
                    catch(e){
                        myselect.add(dayList[i]);
                    }
                }
            }


        </script>
        <script type="text/javascript">
            var count = 0;
            var cells = [];
            function addRow(tableId, cells){
                var tableElem = document.getElementById(tableId);
                var newRow = tableElem.insertRow(tableElem.rows.length);
                var newCell;
                for (var i=0; i<cells.length; i++){
                    newCell =  newRow.insertCell(newRow.cells.length);
                    newCell.innerHTML= cells[i];
                }
                return newRow;
            }
            function deleteRow(tableID,rowNumber){
                var tableElem =  document.getElementById(tableID);
                if(rowNumber>0 && rowNumber < tableElem.rows.length){
                    tableElem.deleteRow(rowNumber);
                }else {
                    alert("Failed");
                }
            }
            function searchNode(node, strSearch, tableName){
                if(node== null){
                    return;
                }
                if(node.tagName == "taskName"){
                    var tmp = node.firstChild.nodeValue;
                    if(tmp.indexOf(strSearch, 0)> -1){
                        var parent = node.parentNode;
                        var firstNode = parent.firstChild;
                        count++;
                        cells[0]= count;

                        cells[1]=firstNode.firstChild.nodeValue; //taskID
                        var sibling = firstNode.nextSibling;
                        cells[2] = sibling.firstChild.nodeValue;// taskName
                        sibling = sibling.nextSibling;
                        sibling = sibling.nextSibling;
                        cells[3] = sibling.firstChild.nodeValue;// user
                        sibling = sibling.nextSibling;
                        cells[4] = sibling.firstChild.nodeValue;// des
                        sibling = sibling.nextSibling;
                        var startDate = "";
                        var sdChild = sibling.childNodes;
                        startDate=startDate+sdChild[0].firstChild.nodeValue+"-"+sdChild[1].firstChild.nodeValue+"-"+sdChild[2].firstChild.nodeValue;
                        cells[5] = startDate;
                        sibling = sibling.nextSibling;
                        var endDate = "";
                        var seChild = sibling.childNodes;
                        endDate=endDate+seChild[0].firstChild.nodeValue+"-"+seChild[1].firstChild.nodeValue+"-"+seChild[2].firstChild.nodeValue;
                        cells[6] = endDate;
                        sibling = sibling.nextSibling;
                        cells[7] = sibling.firstChild.nodeValue;
                        addRow(tableName, cells);
                    }
                }
                var childs = node.childNodes;
                for (var i=0;i<childs.length; i++){
                    searchNode(childs[i], strSearch, tableName);
                }
            }
            function traversalDOMTree(fileName,tableName){
                var tableElem = document.getElementById(tableName);
                var i=1;
                while (i<tableElem.rows.length){
                    deleteRow(tableName, i);
                }
                count = 0;
                var xmlDOM;
                if (window.ActiveXObject){
                    xmlDOM= new ActiveXObject("Microsoft.XMLDOM");
                    xmlDOM.async = false;
                    xmlDOM.load(fileName);
                }else{
                    var parser = new DOMParser();
                    xmlDOM = parser.parseFromString("<tasks><task></task></tasks>","text/xml")
                }
                if (xmlDOM.parseError.errorCode!=0){
                    alert("Error: "+ xmlDoc.parseError.reason);
                }else{
                    searchNode(xmlDOM, myForm.txtTaskNameSearch.value,tableName);
                }
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Staff Page</title>
    </head>
    <body>
        <%
                    String action = request.getParameter("action");
                    if (action != null && action.equals("Login")) {
                        ProjectBLO blo = new ProjectBLO();
                        String fileName = application.getRealPath("/") + "xml/Minestones.xml";
                        blo.getAllMinestoneOfUsername(fileName, (String) session.getAttribute("USERNAME"));

                        ArrayList<OptionForSelect> optionMinestone = new ArrayList<OptionForSelect>();
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db;
                        db = dbf.newDocumentBuilder();
                        Document doc;
                        String exp = "";
                        XPathFactory xps;
                        XPath xp;
                        NodeList node;
                        doc = db.parse(new File(fileName));
                        xps = XPathFactory.newInstance();
                        xp = xps.newXPath();
                        exp = "//minestone";
                        node = (NodeList) xp.evaluate(exp, doc, XPathConstants.NODESET);

                        for (int i = 0; i < node.getLength(); i++) {
                            NodeList minestoneNode = (NodeList) node.item(i);
                            OptionForSelect option = new OptionForSelect();
                            for (int j = 0; j < minestoneNode.getLength(); j++) {
                                Node children = minestoneNode.item(j);
                                if (children.getNodeName().equals("minestoneID")) {
                                    option.setValue(children.getTextContent().trim());
                                }
                                if (children.getNodeName().equals("minestoneName")) {
                                    option.setName(children.getTextContent().trim());
                                }
                            }
                            optionMinestone.add(option);
                        }
                        request.setAttribute("LISTMINESTONE", optionMinestone);
                        //RequestDispatcher rd = request.getRequestDispatcher("staffMainPage.jsp?action=loadFirst");
                        //rd.forward(request, response);
                    }
        %>
        <div id="wrapper">
            <div id="header">
                <div id="user">
                    ${sessionScope.FULLNAME}, <a href="Controller?action=logout">Log out</a>
                </div><!--end login-->
            </div> <!--end header-->

            <div id="body">
                <div id="title">
                    <h2 align="center">Search Page</h2>
                </div>
                <div id="content" align="center">
                    <div id="left">
                        <table id="Function Reference:" width="200px">
                            <tr>
                                <th>Main Functions:</th>
                            </tr>
                            <tr>
                                <td><a href="staffMainPage.jsp">Staff Main Page</a></td>
                            </tr>
                        </table>
                    </div>
                    <div id="right">
                        <div id="titleRight">
                            <h2 align="center">Update Project Information</h2>
                        </div>
                        <div id="contenRight">
                            <form id="myForm">
                                <table id="tableReference" border="1">
                                    <thead>
                                        <tr>
                                            <th>Task Name</th>
                                            <th>Mines tone</th>
                                            <th>Status</th>
                                        </tr>
                                        <tr>
                                            <th>
                                                <input type="text" name="txtTaskNameSearch" value=""/>
                                            </th>
                                            <th>
                                                <select name="ddlMinestone">
                                                    <option value="___">------</option>
                                                    <c:forEach items="${requestScope.LISTMINESTONE}" var="minestone">
                                                        <option value="${minestone.value}"> ${minestone.name} </option>
                                                    </c:forEach>
                                                </select>
                                            </th>   
                                            <th>
                                                <select name="statusSearch">
                                                    <option>on-going</option>
                                                    <option>finish</option>
                                                </select>
                                            </th>
                                        </tr>
                                    </thead>
                                </table>
                                <input type="button" value="Search Task" onclick="traversalDOMTree('xml/Tasks.xml','tableA');" />
                            </form>
                            <table border="1" id="tableA">
                                <tr>
                                    <th>No.</th>
                                    <th>Task ID</th>
                                    <th>Task Name</th>
                                    <th>Staff</th>
                                    <th>Description</th>
                                    <th>Start Date</th>
                                    <th>End Date</th>
                                    <th>Status</th>
                                </tr>
                            </table>
                        </div>
                    </div>

                </div>

            </div><!--end body-->

            <div id="footer">
            </div>
        </div><!--End wrapper-->

    </body>
</html>
